package com.aegame.demo;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.aaee.union.common.AEListener;
import com.aaee.union.common.AESDK;

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends Activity implements View.OnClickListener {

    private static final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewInit();
        AESDK.onCreate(savedInstanceState);
        AESDK.mainInit(this, ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE, new AEListener() {
            @Override
            public void onFinish(Map<String, String> result, boolean isSucess) {
                // 初始化回调
                Log.d(TAG, "AESDK初始化成功");
                if (isSucess) {
                    Log.d(TAG, "初始化成功");
                }
            }
        }, new AEListener() {
            @Override
            public void onFinish(Map<String, String> result, boolean isSucess) {
                //登出回调
                if (isSucess) {
                    Log.d(TAG, "登出成功");
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            //登录
            case R.id.btn1:
                AESDK.login(new AEListener() {
                    @Override
                    public void onFinish(Map<String, String> result, boolean isSucess) {
                        if (isSucess) {
                            String uid = result.get("uid");
                            String token = result.get("token");
                            Log.d(TAG, "登录成功,--uid:" + uid + " --token:" + token);
                        }
                    }
                });
                break;
            //支付
            case R.id.btn2:
                Map<String, String> payParam = new HashMap<>();
                payParam.put("cpuser_id", "123");                          // 游戏方id
                payParam.put("server_id", "10086");                        // 服务器id
                payParam.put("server_name", "10086s");                     // 服务器名称
                payParam.put("role_id", "8554");                           // 角色id
                payParam.put("role_name", "小明");                         // 角色名称
                payParam.put("role_level", "99");                          // 角色等级
                payParam.put("role_sex", "1");                             // 角色性别
                payParam.put("role_power", "5");                           // 角色战力
                payParam.put("role_ticket_count", "1");                    // 角色游戏硬通货币余额(充值获取)
                payParam.put("role_money_count", "1");                     // 角色游戏币余额(非充值获取)
                payParam.put("role_profession_id", "12");                  // 职业id
                payParam.put("role_profession_name", "剑士");              // 职业名称
                payParam.put("vip_level", "1");                            // 角色vip等级
                payParam.put("union_id", "2");                             // 帮派（公会）id
                payParam.put("union_name", "学习公会");                    // 帮派（公会）名称

                payParam.put("order_id", System.currentTimeMillis() + ""); // 订单id
                payParam.put("product_id", "111");                         // 商品id
                payParam.put("product_name", "钻石");                      // 商品名称
                payParam.put("product_des", "60个钻石");                   // 商品描述
                payParam.put("product_amount", "1");                     // 商品总价，单位分，600 = 600分 = 6元RMB
                payParam.put("buy_count", "1");                            // 购买数量
                payParam.put("notify_url", "http://www.baidu.com");        // 支付回调地址
                payParam.put("ext", "透传");                               // 透传参数
                AESDK.pay(payParam, new AEListener() {
                    @Override
                    public void onFinish(Map<String, String> result, boolean isSucess) {
                        // 注意，支付结果以后端为准！
                        if (isSucess) {
                            Log.d(TAG, "支付成功：" + result.get("order_id"));
                        } else {
                            Log.d(TAG, "支付失败：" + result.get("message"));
                        }
                    }
                });
                break;
            //创角上报
            case R.id.btn3:
                AESDK.reportData(createReportData("1"));
                break;
            //升级上报
            case R.id.btn4:
                AESDK.reportData(createReportData("2"));
                break;
            //进入游戏上报
            case R.id.btn5:
                AESDK.reportData(createReportData("3"));
                break;
            //进入副本上报
            case R.id.btn6:
                AESDK.reportData(createReportData("4"));
                break;
            //离开副本上报
            case R.id.btn7:
                AESDK.reportData(createReportData("5"));
                break;
            //切换账号（注销）
            case R.id.btn8:
                // 回调在初始化接口
                AESDK.loginOut();
                break;
            //退出游戏
            case R.id.btn9:
                AESDK.exit(new AEListener() {
                    @Override
                    public void onFinish(Map<String, String> result, boolean isSucess) {
                        if (isSucess) {
                            Log.d(TAG, "退出游戏");
                            MainActivity.this.finish();
                            android.os.Process.killProcess(android.os.Process.myPid());
                            System.exit(0);
                        }
                    }
                });
                break;
        }
    }

    /**
     * 上报类型
     * 1 : 创角
     * 2 ：升级
     * 3 : 进入游戏
     * 4 : 进入副本
     * 5 ：离开副本
     */
    public Map<String, String> createReportData(String type) {
        Map<String, String> roleParam = new HashMap<>();

        roleParam.put("cpuser_id", "123");                          // 游戏方id
        roleParam.put("server_id", "10086");                        // 服务器id
        roleParam.put("server_name", "10086s");                     // 服务器名称
        roleParam.put("role_id", "8554");                           // 角色id
        roleParam.put("role_name", "小明");                         // 角色名称
        roleParam.put("role_level", "99");                          // 角色等级
        roleParam.put("role_sex", "1");                             // 角色性别
        roleParam.put("role_power", "5");                           // 角色战力
        roleParam.put("role_ticket_count", "1");                    // 角色游戏硬通货币余额(充值获取)
        roleParam.put("role_money_count", "1");                     // 角色游戏币余额(非充值获取)
        roleParam.put("role_profession_id", "12");                  // 职业id
        roleParam.put("role_profession_name", "剑士");              // 职业名称
        roleParam.put("vip_level", "1");                            // 角色vip等级
        roleParam.put("union_id", "2");                             // 帮派（公会）id
        roleParam.put("union_name", "学习公会");                    // 帮派（公会）名称

        roleParam.put("create_role_time", "1630655714000");         // 创角时间，毫秒级时间戳
        roleParam.put("report_type", type);                          // 上报类型
        return roleParam;
    }

    private void viewInit() {
        findViewById(R.id.btn1).setOnClickListener(this);
        findViewById(R.id.btn2).setOnClickListener(this);
        findViewById(R.id.btn3).setOnClickListener(this);
        findViewById(R.id.btn4).setOnClickListener(this);
        findViewById(R.id.btn5).setOnClickListener(this);
        findViewById(R.id.btn6).setOnClickListener(this);
        findViewById(R.id.btn7).setOnClickListener(this);
        findViewById(R.id.btn8).setOnClickListener(this);
        findViewById(R.id.btn9).setOnClickListener(this);
    }


    @Override
    protected void onResume() {
        super.onResume();
        AESDK.onResume();
    }

    @Override
    public void onRestart() {
        super.onRestart();
        AESDK.onRestart();
    }

    @Override
    public void onStart() {
        super.onStart();
        AESDK.onStart();
    }

    @Override
    public void onPause() {
        super.onPause();
        AESDK.onPause();
    }

    @Override
    public void onStop() {
        super.onStop();
        AESDK.onStop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        AESDK.onDestroy();
    }

    @Override
    public void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        AESDK.onNewIntent(intent);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        AESDK.onConfigurationChanged(this, newConfig);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        AESDK.onActivityResult(this, requestCode, resultCode, data);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        AESDK.onRequestPermissionsResult(this, requestCode, permissions, grantResults);
    }

}